// Copyright 2009 Thiago H. de Paula Figueiredo
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package br.com.arsmachina.authorization.internal;

import java.util.Map;

import org.apache.tapestry5.ioc.util.StrategyRegistry;

import br.com.arsmachina.authorization.Authorizer;
import br.com.arsmachina.authorization.ReadObjectAuthorizationException;
import br.com.arsmachina.authorization.ReadTypeAuthorizationException;
import br.com.arsmachina.authorization.RemoveObjectAuthorizationException;
import br.com.arsmachina.authorization.RemoveTypeAuthorizationException;
import br.com.arsmachina.authorization.SearchTypeAuthorizationException;
import br.com.arsmachina.authorization.SingleTypeAuthorizer;
import br.com.arsmachina.authorization.StoreTypeAuthorizationException;
import br.com.arsmachina.authorization.UpdateObjectAuthorizationException;
import br.com.arsmachina.authorization.UpdateTypeAuthorizationException;
import br.com.arsmachina.authorization.impl.AllowAllSingleTypeAuthorizer;
import br.com.arsmachina.authorization.impl.DenyAllSingleTypeAuthorizer;

/**
 * Default {@link Authorizer} implementation.
 * 
 * @author Thiago H. de Paula Figueiredo
 */
public class AuthorizerImpl implements Authorizer {

	@SuppressWarnings("unchecked")
	final private StrategyRegistry<SingleTypeAuthorizer> registry;

	/**
	 * Single constructor of this class.
	 * 
	 * @param map a {@link Map}.
	 * @param strict
	 */
	@SuppressWarnings("unchecked")
	public AuthorizerImpl(Map<Class, SingleTypeAuthorizer> map, boolean strict) {

		if (map.get(Object.class) == null) {

			if (strict) {
				map.put(Object.class, new DenyAllSingleTypeAuthorizer(Object.class));
			}
			else {
				map.put(Object.class, new AllowAllSingleTypeAuthorizer(Object.class));
			}

		}

		registry = StrategyRegistry.newInstance(SingleTypeAuthorizer.class, map);

	}

	public boolean canStore(Class<?> clasz) {
		
		if (clasz == null) {
			throw new IllegalArgumentException("Parameter clasz cannot be null");
		}
		
		return getAuthorizer(clasz).canStore();
		
	}

	@SuppressWarnings("unchecked")
	public boolean canUpdate(Class<?> clasz) {
		
		if (clasz == null) {
			throw new IllegalArgumentException("Parameter clasz cannot be null");
		}
		
		final SingleTypeAuthorizer authorizer = getAuthorizer(clasz);
		return authorizer.canUpdate();
		
	}

	@SuppressWarnings("unchecked")
	public boolean canUpdate(Object object) {
		
		if (object == null) {
			throw new IllegalArgumentException("Parameter object cannot be null");
		}
		
		return getAuthorizer(object.getClass()).canUpdate(object);
	}

	public boolean canRemove(Class<?> clasz) {
		
		if (clasz == null) {
			throw new IllegalArgumentException("Parameter clasz cannot be null");
		}
		
		return getAuthorizer(clasz).canRemove();
		
	}

	@SuppressWarnings("unchecked")
	public boolean canRemove(Object object) {
		
		if (object == null) {
			throw new IllegalArgumentException("Parameter object cannot be null");
		}
		
		return getAuthorizer(object.getClass()).canRemove(object);
		
	}

	public boolean canSearch(Class<?> clasz) {
		
		if (clasz == null) {
			throw new IllegalArgumentException("Parameter clasz cannot be null");
		}
		
		return getAuthorizer(clasz).canSearch();
		
	}

	public boolean canRead(Class<?> clasz) {
		
		if (clasz == null) {
			throw new IllegalArgumentException("Parameter clasz cannot be null");
		}
		
		return getAuthorizer(clasz).canRead();
		
	}

	@SuppressWarnings("unchecked")
	public boolean canRead(Object object) {
		
		if (object == null) {
			throw new IllegalArgumentException("Parameter object cannot be null");
		}
		
		return getAuthorizer(object.getClass()).canRead(object);
		
	}

	public void checkRead(Class<?> clasz) {

		if (clasz == null) {
			throw new IllegalArgumentException("Parameter clasz cannot be null");
		}
		
		if (canRead(clasz) == false) {
			throw new ReadTypeAuthorizationException(clasz);
		}
		
	}

	public void checkRead(Object object) {
		
		if (object == null) {
			throw new IllegalArgumentException("Parameter object cannot be null");
		}

		if (canRead(object) == false) {
			throw new ReadObjectAuthorizationException(object);
		}

	}

	public void checkRemove(Class<?> clasz) {
		
		if (clasz == null) {
			throw new IllegalArgumentException("Parameter clasz cannot be null");
		}

		if (canRemove(clasz) == false) {
			throw new RemoveTypeAuthorizationException(clasz);
		}

	}

	public void checkRemove(Object object) {

		if (object == null) {
			throw new IllegalArgumentException("Parameter object cannot be null");
		}
		
		if (canRemove(object) == false) {
			throw new RemoveObjectAuthorizationException(object);
		}

	}

	public void checkSearch(Class<?> clasz) {
		
		if (clasz == null) {
			throw new IllegalArgumentException("Parameter clasz cannot be null");
		}

		if (canSearch(clasz) == false) {
			throw new SearchTypeAuthorizationException(clasz);
		}

	}

	public void checkStore(Class<?> clasz) {
		
		if (clasz == null) {
			throw new IllegalArgumentException("Parameter clasz cannot be null");
		}

		if (canStore(clasz) == false) {
			throw new StoreTypeAuthorizationException(clasz);
		}

	}

	public void checkUpdate(Class<?> clasz) {
		
		if (clasz == null) {
			throw new IllegalArgumentException("Parameter clasz cannot be null");
		}

		if (canUpdate(clasz) == false) {
			throw new UpdateTypeAuthorizationException(clasz);
		}

	}

	public void checkUpdate(Object object) {
		
		if (object == null) {
			throw new IllegalArgumentException("Parameter object cannot be null");
		}

		if (canUpdate(object) == false) {
			throw new UpdateObjectAuthorizationException(object);
		}

	}
	
	@SuppressWarnings("unchecked")
	private SingleTypeAuthorizer getAuthorizer(Class<?> clasz) {
		
		final SingleTypeAuthorizer authorizer = registry.get(clasz);
		return authorizer;
		
	}

}
